package com.github.thread.threadLocal;

/**
 * ThreadLocal局限性
 * ThreadLocal无法在父子线程之间传递
 */
public class ThreadLocalTest1 {
    private static ThreadLocal<Integer> requestIdThreadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        Integer reqId = new Integer(5);
        ThreadLocalTest1 a = new ThreadLocalTest1();
        a.setRequestId(reqId);

    }
    public void setRequestId(Integer requestId) {
        requestIdThreadLocal.set(requestId);
        doBussiness();

    }
    public void doBussiness() {
        System.out.println("首先打印requestId:" + requestIdThreadLocal.get());
        (new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("子线程启动");
                System.out.println("在子线程中访问requestId:" + requestIdThreadLocal.get());
            }

        })).start();

    }

}
